package cn.imnaer.service.rpc.share.dao;

import java.util.List;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import cn.imnaer.common.factory.TimeHelper;
import cn.imnaer.database.BaseDao;
import cn.imnaer.database.MatchConst;
import cn.imnaer.database.define.DBConstant;
import cn.imnaer.service.persistence.model.FriendCircle;
import cn.imnaer.service.persistence.model.SharePhoto;
import cn.imnaer.service.persistence.model.SharePhotoItem;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTO;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTOITEM;
import cn.imnaer.service.rpc.enums.EnumFriendCircleType;
import cn.imnaer.service.rpc.enums.EnumPlaceType;
import cn.imnaer.service.rpc.friendcircle.dao.FriendCircleDaoImpl;

public class SharePhotoDaoImpl extends BaseDao<SharePhoto> {
	private static SharePhotoDaoImpl instance = new SharePhotoDaoImpl();

	public static SharePhotoDaoImpl getInstance() {
		return instance;
	}

	private SharePhotoDaoImpl() {

	}

	@Override
	protected String getDbSpaceName() {
		return DBConstant.SPACE.SHAREPHOTO_INFO_SPACE;
	}

	@Override
	protected String getDbCollectionName() {
		return DBConstant.COLLECTION.SHAREPHOTO_INFO_COLLECTION;
	}

	@Override
	protected String getPrimaryKey() {
		return SHAREPHOTO.sharePhotoId;
	}

	@Override
	public synchronized void generateId(SharePhoto obj) {
		// TODO Auto-generated method stub
		super.generateId(obj);
		String sharePhotoId = obj.getSharePhotoId();
		List<SharePhotoItem> items = obj.getItems();
		int size = items.size();
		for (int i = 0; i < size; i++) {
			SharePhotoItem sharePhotoItem = items.get(i);
			sharePhotoItem.setSharePhotoItemId(sharePhotoId + i);
		}
	}

	public SharePhoto findBySharePhotoItemId(String itemId) {

		BSONObject matcher = new BasicBSONObject();
		matcher.put(SHAREPHOTO.items + "." + SHAREPHOTOITEM.sharePhotoItemId, itemId);
		return this.findOne(matcher);

	}
	@Override
	protected void beforeSave(SharePhoto sharePhoto) {
		sharePhoto.setCreateTime(TimeHelper.getCurrentTime());
	}
}
